package com.heyqing.topic.MajorityElement_0169;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Scanner;

/**
 * ClassName:MajorityElement
 * Package:com.heyqing.topic.MajorityElement_0169
 * Description:
 *
 * @Date:2024/9/19
 * @Author:Heyqing
 */
public class MajorityElement {
    public int majorityElement(int[] nums) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int t = nums[i];
            Integer val = map.get(t);
            if (Objects.nonNull(val)) {
                val++;
                map.put(t, val);
            } else {
                map.put(t, 1);
            }
        }
        Integer maxKey = map.entrySet().stream().max(Map.Entry.comparingByValue()).map(Map.Entry::getKey).orElse(null);
        return maxKey;
    }

    public static void start() {
        Scanner sc = new Scanner(System.in);
        System.out.println("len:");
        int len = sc.nextInt();
        int[] nums = new int[len];
        for (int i = 0; i < len; i++) {
            nums[i] = sc.nextInt();
        }
        MajorityElement me = new MajorityElement();
        int res = me.majorityElement(nums);
        System.out.println(res);
    }
}
